//
//  YXPhotoListViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-5-5.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXPhotoListViewController.h"
#import "YXPhotoListTableViewCell.h"

@interface YXPhotoListViewController ()

@end

@implementation YXPhotoListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_scrollBackBtn removeFromSuperview];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    YXPhotoListView * photoListVi = [[YXPhotoListView alloc] init];
    self.view = photoListVi;
    photoListVi.tabView.delegate = self;
    photoListVi.tabView.dataSource = self;
    [photoListVi.takeAPictureBtn addTarget:self action:@selector(touchTakeAPictureBtn) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - ButtonsClick
- (void)touchTakeAPictureBtn
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController * cameraPicker = [[UIImagePickerController alloc] init];
        cameraPicker.delegate = self;
        cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        cameraPicker.allowsEditing = YES;
        [self presentViewController:cameraPicker animated:YES completion:nil];
    }else{
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"你的设备不支持相机功能."];
    }
}

- (void)touchScrollBackBtn
{
    _scrollBackBtn.hidden = YES;
}

- (void)touchDeleteBtn:(UIButton *)btn
{
    UIAlertView * alertVi = [[UIAlertView alloc] initWithTitle:@"删除" message:@"确定删除该照片？" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",@"取消", nil];
    [alertVi show];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
    }
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * identifier = @"cell";
    YXPhotoListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[YXPhotoListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell.deleteBtn addTarget:self action:@selector(touchDeleteBtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.deleteBtn.tag = indexPath.row + 1;
    cell.dateLab.text = @"05-05 12:12";
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (!_scrollBackBtn) {
        UIWindow * window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        _scrollBackBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _scrollBackBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
        _scrollBackBtn.frame = [UIScreen mainScreen].bounds;
        [_scrollBackBtn addTarget:self action:@selector(touchScrollBackBtn) forControlEvents:UIControlEventTouchDown];
        [window addSubview:_scrollBackBtn];
        
        UIScrollView * photoScrollVi = [[UIScrollView alloc] init];
        photoScrollVi.bounces = NO;
        photoScrollVi.bounds = CGRectMake(0, 0, 320, 320);
        photoScrollVi.center = window.center;
        photoScrollVi.backgroundColor = [UIColor blackColor];
        photoScrollVi.pagingEnabled = YES;
        photoScrollVi.delegate = self;
        photoScrollVi.contentSize = CGSizeMake(640, [UIScreen mainScreen].bounds.size.height);
        [_scrollBackBtn addSubview:photoScrollVi];
        
        _pageLab = [[UILabel alloc] initWithFrame:CGRectMake(210, photoScrollVi.frame.origin.y + photoScrollVi.frame.size.height + 20, 90, 15)];
        _pageLab.text = @"0/0";
        _pageLab.textAlignment = NSTextAlignmentCenter;
        _pageLab.textColor = [UIColor whiteColor];
        _pageLab.font = [UIFont boldSystemFontOfSize:16];
        [_scrollBackBtn addSubview:_pageLab];
    }
    _scrollBackBtn.hidden = NO;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
